﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;
using TuanAnhPersonalWeb.Classes;

namespace TuanAnhPersonalWeb.Areas.Admin.Controllers
{
       [Authorize(Roles = "admin,employee")]
    public class PositionController : Controller
    {
        private TuanAnhEntities db = new TuanAnhEntities();
        PositionAccessConsolidator access = new PositionAccessConsolidator();
        

        //
        // GET: /Admin/Position/
          
        public ActionResult Index()
        {
            IList<PositionModel> model = access.getAllPosition();
            return View(model);
        }

        //
        // GET: /Admin/Position/Details/5
          
        public ViewResult Details(int id)
        {
            TUANANH__POSITION tuananh__position = db.TUANANH__POSITION.Single(t => t.POS_ID == id);
            return View(tuananh__position);
        }

        //
        // GET: /Admin/Position/Create
            
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Admin/Position/Create
        
        [HttpPost]
        public ActionResult Create(TUANANH__POSITION tuananh__position)
        {
            if (ModelState.IsValid)
            {
                db.TUANANH__POSITION.AddObject(tuananh__position);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(tuananh__position);
        }
        
        //
        // GET: /Admin/Position/Edit/5
          
        public ActionResult Edit(int id)
        {
            PositionModel model = access.getPositionID(id);
            var data = access.getAllEmploy(id);
            int i = data.Count();
            if (i != 0)
            {

                ViewData["EMP"] = new SelectList(access.getAllEmploy(id), "EMP_ID", "EMP_NAME", model.LEADER_ID);
                return View(model);
            }
            else
                return View("NoEmp");
            
            
        }

        //
        // POST: /Admin/Position/Edit/5
         
        [HttpPost]
        public ActionResult Edit(PositionModel model)
        {
            if (ModelState.IsValid)
            {
                access.UpdatePosition(model);
                return RedirectToAction("Index");
            }
            ViewData["EMP"] = new SelectList(access.getAllEmploy(model.POS_ID.Value), "EMP_ID", "EMP_NAME", model.LEADER_ID);
            return View(model);
        }

        //
        // GET: /Admin/Position/Delete/5
          
        public ActionResult Delete(int id)
        {
            TUANANH__POSITION tuananh__position = db.TUANANH__POSITION.Single(t => t.POS_ID == id);
            return View(tuananh__position);
        }

        //
        // POST: /Admin/Position/Delete/5
        
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            TUANANH__POSITION tuananh__position = db.TUANANH__POSITION.Single(t => t.POS_ID == id);
            db.TUANANH__POSITION.DeleteObject(tuananh__position);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}